﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Game_Controller : MonoBehaviour {

    [Header(" Player ")]
    public GameObject Player;
    Vector3 ToothBrushInitialScale;

    [Header(" Canvas Groups ")]
    public CanvasGroup MENU_CG;
    public CanvasGroup GAME_CG;

    [Header(" Static Variables ")]
    public static bool gameOn = false;
    public static bool gameOverTrigger = false;

    [Header(" Balls Settings ")]
    public Container_Manager CM;

    [Header("Score & Best")]
    public Text T_Score;
    public Text T_Best;
    public static int SCORE;
    public static int BEST;


	// Use this for initialization
	void Start () {

        ToothBrushInitialScale = Player.transform.localScale;

        //Set the menu
        SetMenu();

        BEST = PlayerPrefs.GetInt("BEST");
	}
	
	// Update is called once per frame
	void Update () {


        if(gameOverTrigger){

            SetGameOver();
            gameOverTrigger = false;
        }

        //Update the scores
        if (gameOn)
        {
            T_Score.text = "" + SCORE;

			if (SCORE >= BEST)
			{
				BEST = SCORE;
				PlayerPrefs.SetInt("BEST", BEST);
				T_Best.text = "" + BEST;
			}
        }
            else
        {
            if(SCORE >= BEST)
            {
                BEST = SCORE;
                PlayerPrefs.SetInt("BEST", BEST);
                T_Best.text = "" + BEST;
            }
        }
	}


    public void SetMenu(){

        //Set the CG
        EnableCG(MENU_CG);
        DisableCG(GAME_CG);

        //Set the Game as On
        gameOn = false;
    }

    public void SetGame(){

		//Set the CG
        EnableCG(GAME_CG);
        DisableCG(MENU_CG);

		//Set the Game as On
		gameOn = true;

        //Reset the Size of the toothBrush
        Player.transform.localScale = ToothBrushInitialScale;

        //Reset balls
        CM.ResetBallsAmount();

        //Reset Score
        SCORE = 0;

    }

    void SetGameOver(){

        //Back to the menu
        SetMenu();

        //Game Not On
        gameOn = false;

    }



	/****************************/ // UTILITIES // /****************************/


    void EnableCG(CanvasGroup cg){
        cg.alpha = 1;
        cg.interactable = true;
        cg.blocksRaycasts = true;
    }

    void DisableCG(CanvasGroup cg){
        cg.alpha = 0;
        cg.interactable = false;
        cg.blocksRaycasts = false;
    }





	/***************************************************************************/
}
